package com.kim.whereeat.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.kim.whereeat.model.FoodComment;
import com.kim.whereeat.utils.SQLiteHelper;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 伟阳 on 2015/10/24.
 */
public class FoodCommentService {

    private SQLiteHelper helper;
    private static final String TABLENAME = "foodcomment";

    public FoodCommentService(Context context) {
        helper = new SQLiteHelper(context);
    }

    /**
     * 判断用户是否已评论
     *
     * @param user_id 用户ID
     * @param food_id 食品ID
     * @return true已评论/false未评论
     */
    public boolean exists(int user_id, int food_id) {
        boolean flag = false;
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, null, "user_id=? and food_id=?", new String[]{user_id + "", food_id + ""}, null, null, null);
        flag = cursor.getColumnCount() > 0;
        cursor.close();
        return flag;
    }

    /**
     * 添加评论
     *
     * @param foodComment 评论
     * @return true评论成功/false评论失败
     */
    public boolean addComment(FoodComment foodComment) {
        SQLiteDatabase dbWriter = helper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("food_id", foodComment.getFood_id());
        cv.put("user_id", foodComment.getUser_id());
        cv.put("title", foodComment.getTitle());
        cv.put("content", foodComment.getContent());
        cv.put("time", foodComment.getTime());
        cv.put("star", foodComment.getStar());
        long temp = dbWriter.insert(TABLENAME, null, cv);
        return temp > 0;
    }

    /**
     * 获取食品平均评分
     *
     * @param food_id 食品ID
     * @return 食品平均评分
     */
    public double getAverageStarById(int food_id) {
        double avg = 0;
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, new String[]{"AVG(star)"}, "food_id=?", new String[]{food_id + ""}, null, null, null);
        if (cursor.moveToNext()) {
            avg = cursor.getDouble(0);
        }
        cursor.close();
        return avg;
    }

    /**
     * 获取评论列表
     *
     * @param food_id 食品ID
     * @return 评论列表
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public List<FoodComment> getFoodCommentListById(int food_id) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        List<FoodComment> foodCommentList = new ArrayList<>();
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, null, "food_id=?", new String[]{food_id + ""}, null, null, null);
        int cols_len = cursor.getColumnCount();
        while (cursor.moveToNext()) {
            FoodComment foodComment = FoodComment.class.newInstance();
            for (int i = 1; i <= cols_len; i++) {
                String cols_name = cursor.getColumnName(i);
                Object cols_value = cursor.getString(cursor.getColumnIndex(cols_name));
                if (cols_value == null) {
                    cols_value = "";
                }
                Field field = FoodComment.class.getDeclaredField(cols_name);
                field.setAccessible(true);
                field.set(foodComment, cols_value);
            }
            foodCommentList.add(foodComment);
        }
        cursor.close();
        return foodCommentList;
    }

    /**
     * 关闭数据库
     */
    public void close() {
        if (helper != null)
            helper.close();
    }
}
